<?php

/**
 * web服务器api接口入口页面
 * @author 张云江
 */
//初始化服务器时间为中国时间
date_default_timezone_set("PRC");

//宏定义
define("DIR", dirname(__FILE__) . DIRECTORY_SEPARATOR);
define("MEMFRAMEWORKDIR", "../MemFrameWork/");

//包含文件
include_once(DIR . MEMFRAMEWORKDIR . "MemLib/Mlog.class.php");
include_once(DIR . MEMFRAMEWORKDIR . "config.mem.php");
require_once(DIR . 'config/config.inc.php');
include_once(DIR . "lib/com_code.php");
require_once(DIR . "lib/PackBase.class.php");
require_once(DIR . 'lib/PublicFunc.class.php');
include_once(DIR . 'lib/ModelOutPutInfo.class.php');
include_once(DIR . "lib/log.class.php");

//记录生产环境下错误日志
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

//在global文件中引入Mlog文件，同时进行日志配置初始化
$mlog_conf = \Config\ServerConfig::getConfigWithHost("mlog_conf");
$logdir = \Mlog::setConfig($mlog_conf['client_api'], $mlog_conf['all_conf']);

ini_set('error_log', $logdir);
$start_time = microtime(true);
\Mlog::fatal(""); //用于生成日志文件
ini_set('error_log', $logdir);

if (isset($_REQUEST["debug"]) && $_REQUEST["debug"] === "true") {
    $GLOBALS["is_debug"] = true;
} else {
    //    error_reporting(0);
}
$jPack = $_POST["json"] ? $_POST["json"] : $_GET["json"];

$jsonObj = json_decode($jPack);

//记录传入参数日志
\Mlog::info("Request:" . print_r($jPack, true));

$outModel = ModelOutPutInfo::getInstance();
$outModel->AddOutInfo("op_type", $jsonObj->op_type);
$outModel->AddOutInfo("code", "-1");
if (!$GLOBALS['serverCfg']["serverIsOpen"]) {
    $outModel->AddOutInfo("code", $GLOBALS['serverCfg']['closeMessage'][0]['code']);
    $outModel->AddOutInfo("msg", $GLOBALS['serverCfg']['closeMessage'][0]['msg']);
} else {
    $files = array(
        "class_file" => DIR . "app/class/{$jsonObj->op_type}.class.php",
    );
    foreach ($files as $each) {
        if (file_exists($each)) {
            include $each;
        }
    }
    $class_name = "Api_" . $jsonObj->op_type;
    if (class_exists($class_name)) {
        $obj = new $class_name($jsonObj);
        if (method_exists($obj, "run")) {
            $obj->run();
        } else {
            $outModel->AddOutInfo("notice", "not found method run ");
        }
    } else {
        $outModel->AddOutInfo("notice", "not found class $class_name or not found the {$jsonObj->op_type} interface");
    }
}
$returnValue = $outModel->outPutJson();
//记录返回参数日志
\Mlog::info("Return:" . json_encode(json_decode($returnValue), JSON_UNESCAPED_UNICODE));

PublicFunc::gzip_encode($returnValue);
